$(document).ready(function(){
	  // 在这里写你的代码...
	var contextPath = Com_Parameter.ContextPath;
	//分页函数
	var pagination = function(url,pageNo,pageSize,supplierNo,supplierName){
		
		$.ajax({
			type: "GET",
			url:  url+"?pageSize="+pageSize+"&pageNo="+pageNo,
			data: {"cname":supplierName,"supplierNo":supplierNo},
			success: function(data) {
				var supplierObj = eval('(' + data + ')');
				var supplierList = supplierObj.jsonRoot;
				$("#supplierTbody").find("tr").each(function(){
					$(this).remove();
				});
				var tbody =$("#supplierTbody")
				for(var i=0;i<supplierList.length;i++ ){
					var supplier = supplierList[i];
					var tr = "<tr><td o-l-tab-ckb>" ;
						tr +="<input type=\"checkbox\" data-id=\""+supplier.id+"\" title=\""+i+"\" >" ;
						tr +="<input type=\"hidden\" id=\"cname"+i+"\" value=\""+supplier.cname+"\">" ;
						tr +="<input type=\"hidden\" id=\"supplierType"+i+"\" value=\""+supplier.supplierType+"\">" ;
						tr +="<input type=\"hidden\" id=\"supplierLevel"+i+"\" value=\""+supplier.supplierLevel+"\">" ;
						tr +="<input type=\"hidden\" id=\"status"+i+"\" value=\""+supplier.status+"\">" ;
						tr +="<input type=\"hidden\"  id=\"quality"+i+"\" value=\""+supplier.quality+"\">" ;
						tr +="<input type=\"hidden\"  id=\"supplierNo"+i+"\" value=\""+supplier.supplierNo+"\">";
						tr +="</td>" ;
						tr +="<td class=\"o-l-tab-ckb\">"+supplier.supplierNo+"</td>" ;
						tr +="<td class=\"o-l-tab-ckb\">"+supplier.cname+"</td>" ;
						tr +="<td class=\"o-l-tab-ckb\">"+supplier.supplierTypeValue+"</td>" ;
						tr +="<td class=\"o-l-tab-ckb\">"+supplier.levelValue+"</td>" ;
						tr +="<td class=\"o-l-tab-ckb\">"+supplier.statusValue+"</td>" ;
						tr +="<td class=\"o-l-tab-ckb\">"+supplier.qualityValue+"</td>" ;
						tr +="</tr>";
						tbody.append(tr);
				}
				
				var total =parseFloat(supplierObj.total);
				var totalSize = Math.ceil(total/parseFloat(pageSize));
				$("#pageNo").val(supplierObj.pageNo);
				$("#pageSize").val(supplierObj.pageSize);
				$("#total").val(supplierObj.total);
				$("#pageNoSpan").text("第"+supplierObj.pageNo+"页");
				$("#totalSizeSpan").text("共"+totalSize+"页");
				if(supplierObj.pageNo ==1){
					$("#lastPageLi").attr("class","disabled");
				}else if(supplierObj.pageNo >=totalSize){
					$("#nextPageLi").attr("class","disabled");
				}else{
					$("#lastPageLi").attr("class","");
					$("#nextPageLi").attr("class","");
				}
				
			}
		});
	}
	/**************************************选择供应商*************************************************************************/
		$("#SupplierOk").click(function(){
			$("#replaceSupplierName").focus();
			var ids = new Array(), i = 0;
			var title;
			$('#SupplierTable').find("input[type='checkbox'][data-id]").each(function(){
				var $this = $(this);
				if($this.prop('checked')){
					ids.push($(this).attr("data-id"))  ;
					title = $(this).attr("title");
				}
			});
			if(ids.length < 1){
				top.$.jBox.tip('请选择一条供应商信息','warning');
				return;
			}
			if(ids.length > 1){
				top.$.jBox.tip('只能选择一条供应商信息','warning');
				return;
			}
			var id = ids[0];
			$("#SupplierOk").attr("data-dismiss","modal");
			$.get(contextPath+"/biz/contract/commonContract/"+id+"/getSupplierRegister", function(data){
				var jsonObj =eval('(' + data + ')'); 
				var item = jsonObj.taxType;
				var taxTypeText;
				var taxTypeValue;
				$("#taxTypeCode_select").children("option").each(function(){
					if($(this).val() == item){
						$(this).attr("selected","selected");
						taxTypeText = $(this).text();
						taxTypeValue= $(this).attr("value");
						$("#taxTypeCode").val(taxTypeValue)
						$("#taxType").val(taxTypeText)
					    return false; 
					}else{
						$("#taxTypeCode_select").val('');
						$("#taxTypeCode").val('')
						$("#taxType").val('')
						taxTypeText='NAN'
					}
				});
				$.get(contextPath+"/biz/contract/commonContract/"+taxTypeValue+"/getTaxRateTypeCode",function(data){
					var jsonObj =eval('(' + data + ')'); 
					if(jsonObj.length==0){
						$("#taxRateCode_select").empty(); 
					}
					$("#taxRateCode_select").children("option").each(function(){
						$(this).remove();	
					});
					for(var i = 0 ;i<jsonObj.length;i++){
						var tax = jsonObj[i];
						$("#taxRateCode_select").append("<option value='"+tax.taxRateNum+"'>"+tax.taxRate+"</option>");
						if(i==1){
							$("#taxRateCode").val(tax.taxRateNum);
							$("#taxRate").val(tax.taxRate);
						}
					}
				});
			});

			var cname = $("#cname"+title).val();
			var supplierNo = $("#supplierNo"+title).val();
			var supplierType = $("#supplierType"+title).val();
			var supplierLevel = $("#supplierLevel"+title).val();
			var status = $("#status"+title).val();
			var quality = $("#quality"+title).val();
			var terminate = $("#terminate").attr("value");
			if(terminate !="terminate"){
				$("#supplierId").val(id);
				$("#supplierName").val(cname);
				$("#supplierNo").val(supplierNo);
			}
				$("#replaceSupplierName").val(cname);
				$("#replaceSupplierId").val(id);
				$("#replaceSupplierNo").val(supplierNo);
		});
		
		/*********************************分页查询***********************************************/
		//条件查询
		$("#querySupplier").click(function(){
			var supplierNo = $("#supplierno").val();
			var supplierName = $("#suppliername").val();
			var pageNo = 1;
			var url =contextPath+"/biz/brand/supplier/suppInfoList";
			var pageSize = parseFloat($("#pageSize").val());
			pagination(url,pageNo,pageSize,supplierNo,supplierName);
		});
		//跳转页面输入框只能输入正整数
		$("#gotoPageNo").keyup(function(){
			var str=$(this).val();
			var newstr='';
			 for(i=0;i<str.length;i++){
				 var j=str.charCodeAt(i);
				 if(j>47&&j<55){
					 newstr+=String.fromCharCode(j); 
				 }
			  }
			 if( newstr.charCodeAt(0)==48 ){
				 $(this).val(newstr.substr(1,newstr.length)); 
			 } else{ 
				 $(this).val(newstr); 
			}
		});
		//跳转到指定页面
		$("#goto").click(function(){
			var supplierNo = $("#supplierno").val();
			var supplierName = $("#suppliername").val();
			var pageNo = $("#gotoPageNo").val();
			var pageSize = parseFloat($("#pageSize").val());
			var total =parseFloat($("#total").val());
			var totalSize = Math.ceil(total/pageSize);
			var url =contextPath+"/biz/brand/supplier/suppInfoList";
			if(pageNo ==null && ''==pageNo){
				top.$.jBox.tip('请输入页码','warning');
				return;
			}else if(pageNo >totalSize ||pageNo<1){
				top.$.jBox.tip('你输入的页码不在范围内','warning');
				return;
			}
			pagination(url,pageNo,pageSize,supplierNo,supplierName);
		});
		//上一页
		$("#lastPage").click(function(){
			var supplierNo = $("#supplierno").val();
			var supplierName = $("#suppliername").val();
			var pageNo = parseFloat($("#pageNo").val());
			var pageSize = parseFloat($("#pageSize").val());
			var total =parseFloat($("#total").val());
			var nextPageNo = pageNo + 1;
			var lastPageNo = pageNo -1;
			var totalSize = Math.ceil(total/pageSize);
			var url =contextPath+"/biz/brand/supplier/suppInfoList";
			if(pageNo <=1){
				top.$.jBox.tip('没有上一页','warning');
				return;
			}
			pagination(url,lastPageNo,pageSize,supplierNo,supplierName);
		});
		//下一页
		$("#nextPage").click(function(){
			var supplierNo = $("#supplierno").val();
			var supplierName = $("#suppliername").val();
			var pageNo = parseFloat($("#pageNo").val());
			var pageSize = parseFloat($("#pageSize").val());
			var total =parseFloat($("#total").val());
			var nextPageNo = pageNo + 1;
			var totalSize = Math.ceil(total/pageSize);
			var url =contextPath+"/biz/brand/supplier/suppInfoList";
			if(pageNo >=totalSize){
				top.$.jBox.tip('没有下一页','warning');
				return;
			}
			pagination(url,nextPageNo,pageSize,supplierNo,supplierName);
		});
	});